package com.irdstudio.bsp.executor.core.plugin.ftp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据表操作类-[表名: 01.数据装载配置表(plugin_ftp_conf)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-04-29
 */
public class PluginFtpConfDao {

	/* 连接对象 */
	Connection conn = null;	

	public PluginFtpConfDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public PluginFtpConf queryWithKeys(String pluginConfId,String confSort)
			throws SQLException {
		PluginFtpConf fc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_load_conf WHERE plugin_conf_id=? and conf_sort=?");
			ps.setString(1,pluginConfId);
			ps.setString(2,confSort);
			rs = ps.executeQuery();
			if (rs.next()) {
				fc = new PluginFtpConf();
				fc.setPluginConfId(rs.getString("plugin_conf_id"));
				fc.setConfSort(rs.getInt("conf_sort"));
				fc.setSftpServerIp(rs.getString("sftp_server_ip") );
				fc.setFtpServerPort(rs.getInt("ftp_server_port"));
				fc.setFtpServerUser(rs.getString("ftp_server_user"));
				fc.setFtpServerPwd(rs.getString("ftp_server_pwd"));
				fc.setLoadType(rs.getString("load_type"));
				fc.setRemoteFileName(rs.getString("remote_file_name"));
				fc.setLocalFileName(rs.getString("local_file_name"));
				fc.setFtpFilePath(rs.getString("ftp_file_path"));
				fc.setLocalFilePath(rs.getString("local_file_path"));
				fc.setCheckExist(rs.getString("check_exist"));
				fc.setServerType(rs.getString("server_type"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginFtpConfWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return fc;
	}

	/**
	   * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<PluginFtpConf> queryWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<PluginFtpConf> fcList = new ArrayList<PluginFtpConf>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM plugin_ftp_conf "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				PluginFtpConf fc = new PluginFtpConf();
				fc.setPluginConfId(rs.getString("plugin_conf_id"));
				fc.setConfSort(rs.getInt("conf_sort"));
				fc.setSftpServerIp(rs.getString("sftp_server_ip") );
				fc.setFtpServerPort(rs.getInt("ftp_server_port"));
				fc.setFtpServerUser(rs.getString("ftp_server_user"));
				fc.setFtpServerPwd(rs.getString("ftp_server_pwd"));
				fc.setLoadType(rs.getString("load_type"));
				fc.setRemoteFileName(rs.getString("remote_file_name"));
				fc.setLocalFileName(rs.getString("local_file_name"));
				fc.setFtpFilePath(rs.getString("ftp_file_path"));
				fc.setLocalFilePath(rs.getString("local_file_path"));
				fc.setCheckExist(rs.getString("check_exist"));
				fc.setServerType(rs.getString("server_type"));
				
				fcList.add(fc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginFtpConfWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return fcList;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
